/**
 * Created by lghdb on 2018/7/10.
 * 操作配置文件
 */

var fs = require('fs');

function load(path, env) {
    var config = JSON.parse(fs.readFileSync(path));
    var defaultConfig = {};
    for (var key in config) {
        if (key == 'modes') continue;
        defaultConfig[key] = config[key]
    }
    var modes = config.modes;
    for (var key in modes) {
        if (modes[key].port == undefined) {
            modes[key].port = config.port;
        }
    }
    var mode = modes[env];
    for (var key in defaultConfig) {
        if (key == 'port') continue;
        mode[key] = defaultConfig[key];
    }
    if (!global.appConfig){
        global.appConfig = mode;
    }
}

module.exports = {
    get: (key) => {
        return  global.appConfig[key];
    },
    load: load,
}